<template>
    <div style="background-color: white;width: 100%;height: 100%;">
        <el-button link @click="ResourceShow()" style="margin-left: 10px;">‹返回</el-button>
        &nbsp;
        <span>|</span>
        &nbsp;
        <span style="font-size: 17px;font-weight: 700;">资源编辑(🛠️)</span>
        <hr style='background-color:#d3d3d3; height:1px; border:none;'>
        
        <el-form  class="demo-ruleForm" :model="form" ref="formRef" label-width="auto" style="max-width: 1000px;margin-left: 180px;margin-top: 30px;">
            <el-form-item label="资源编码" style="margin: 25px;" prop="resourceNo" :rules="[ { required: true, message: '必填' },]">
                <el-input v-model="form.resourceNo"  />
            </el-form-item>
            <el-form-item label="资源名称" style="margin: 25px;" prop="resourceName" :rules="[ { required: true, message: '必填' },]">
                <el-input v-model="form.resourceName" />
            </el-form-item>

            <el-form-item label="资源类别" style="margin: 25px;" >
            <el-select v-model="form.resourceTypeId" >
                <el-option label="请选择" :value="0" />
                <el-option v-for="item in options" :label="item.resourceTypeName" :value="item.resourceTypeId"/>
            </el-select>
            </el-form-item>

            <el-form-item label="资源描述" style="margin: 25px;">
                <el-input v-model="form.resourceDescription" />
            </el-form-item>

            <el-form-item label="资源单位" style="margin: 25px;" prop="resourceUnit" :rules="[ { required: true, message: '必填' },]">
            <el-select v-model="form.resourceUnit" placeholder="请选择">
                <el-option label="个/台" value="个/台" />
                <el-option label="次" value="次" />
                <el-option label="克" value="克" />
                <el-option label="小时" value="小时" />
                <el-option label="千克" value="千克" />
                <el-option label="毫升" value="毫升" />
                <el-option label="人" value="人" />
            </el-select>
            </el-form-item>

            <el-form-item label="计算成本" style="margin: 25px;" prop="resourceCost" :rules="[ { required: true, message: '必填' },]">
            <el-select v-model="form.resourceCost" placeholder="请选择">
                <el-option label="是" value="是" />
                <el-option label="否" value="否" />
            </el-select>
            </el-form-item>

            <el-form-item label="费用类型" style="margin: 25px;">
            <el-select v-model="form.expensesId">
                <el-option label="请选择" :value="0" />
                <el-option v-for="item in options1" :label="item.expensesName" :value="item.expensesId"/>
            </el-select>
            </el-form-item>

            <el-form-item label="计算方式" style="margin: 25px;">
            <el-select v-model="form.resourceCalculation" placeholder="请选择">
                <el-option label="手工" value="手工" />
                <el-option label="采购自动统计" value="采购自动统计" />
                <el-option label="车间自动统计" value="车间自动统计" />
            </el-select>
            </el-form-item>

            <el-form-item label="基准类型" style="margin: 25px;">
            <el-select v-model="form.benchmarkId" placeholder="请选择">
                <el-option label="请选择" :value="0" />
                <el-option v-for="item in options2" :label="item.benchmarkName" :value="item.benchmarkId"/>
            </el-select>
            </el-form-item>

            <el-form-item label="单位成本" style="margin: 25px;">
                <el-input v-model="form.resourcePrice" />
            </el-form-item>

            <el-form-item label="状态" style="margin: 25px;">
            <el-select v-model="form.resourceState" placeholder="请选择">
                <el-option label="有效" :value="true" />
                <el-option label="无效" :value="false" />
            </el-select>
            </el-form-item>

            <el-button style="margin-left: 820px;" @click="ResourceShow">返回</el-button>
            <el-button type="success" style="width: 83px;" @click="submitForm(formRef)">保存</el-button>
            
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import { ref,reactive,onMounted } from 'vue'
import  { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'
import {useRouter,useRoute} from 'vue-router'
import axios from 'axios';
const formRef = ref<FormInstance>()

//动态路由
const apiurls = import.meta.env.VITE_APP_API_URL_ProductionAdd;
const apiurl = import.meta.env.VITE_APP_API_URL_ProductionShow;

const router = useRouter()
const route = useRoute()

const form = reactive({
    resourceNo:'',
    resourceName:'',
    resourceTypeId:0,
    resourceDescription:'',
    resourceUnit:'',
    resourceCost:'',
    expensesId:0,
    resourceCalculation:'',
    benchmarkId:0,
    resourcePrice:0,
    resourceState:true,
    approvalStatus:0,
    isDelete:false,
    handle:'张三',
    handleContext:'添加',
    handleTime:'2024-12-14',
    pid:0,
})

//资源类型
const options = ref([])
const GetResourceType=()=>{
    axios.get(apiurl+'/api/ShowProduction/GetResourceTypeShow').then((res)=>{
        options.value = res.data.data
    })
}

//费用类型
const options1= ref([])
const GetExpenses=()=>{
    axios.get(apiurl+'/api/ShowProduction/GetExpensesShow').then((res)=>{
        options1.value = res.data.data
    })
}

//基准类型
const options2= ref([])
const Getbenchmar=()=>{
    axios.get(apiurl+'/api/ShowProduction/GetBenchmarkShow').then((res)=>{
        options2.value = res.data.data
    })
}

const submitForm=(formEl: FormInstance | undefined)=>{
    if (!formEl) return
    formEl.validate((valid) => {
      if (valid) {
        console.log('submit!')
        return;
      } else {
        console.log('error submit!')
        return;
      }
    })
    ResourAdd()
}


//添加
const ResourAdd=()=>{
    if(form.resourceNo==''){
        return;
    }
    if(form.resourceName==''){
        return;
    }
    if(form.resourceUnit==''){
        return;
    }
    if(form.resourceCost==''){
        return;
    }
    axios.post(apiurls+'/api/ProductionLeo/GetResourceAdd',{
        resourceNo:form.resourceNo,
        resourceName:form.resourceName,
        resourceTypeId:form.resourceTypeId,
        resourceDescription:form.resourceDescription,
        resourceUnit:form.resourceUnit,
        resourceCost:form.resourceCost,
        expensesId:form.expensesId,
        resourceCalculation:form.resourceCalculation,
        benchmarkId:form.benchmarkId,
        resourcePrice:form.resourcePrice,
        resourceState:form.resourceState,
        approvalStatus:form.approvalStatus,
        isDelete:form.isDelete,
        handle:form.handle,
        handleContext:form.handleContext,
        handleTime:form.handleTime,
        pid:form.pid,
        
    }).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '保存成功',
                type: 'success',
            })
            ResourceShow()
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}



const ResourceShow = () => {
    router.push('/ResourceShow')
}

onMounted(()=>{
    GetResourceType()
    GetExpenses()
    Getbenchmar();
})
</script>